package com.acane.instagram.feature.main

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.viewmodel.compose.viewModel
import com.acane.instagram.core.design.component.MyNavigationBar
import com.acane.instagram.feature.add.AddMediaRoute
import com.acane.instagram.feature.home.HomeRoute
import com.acane.instagram.feature.home.HomeViewModel
import com.acane.instagram.feature.me.MeRoute
import com.acane.instagram.feature.me.MeViewModel
import com.acane.instagram.feature.search.SearchRoute
import com.acane.instagram.feature.video.VIDEO_ROUTE
import com.acane.instagram.feature.video.VideoRoute
import kotlinx.coroutines.launch

@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
@Composable
fun MainRoute(
    toProfile: (String) -> Unit,
    toLogin: () -> Unit,
    toMain: () -> Unit,
    toFriend: () -> Unit,
    meViewModel: MeViewModel = viewModel(),
    toMe: () -> Unit,
    toPost: (String, Int) -> Unit,
    toNotification: () -> Unit,
    toFollow: (String, String) -> Unit,
    toSearchDetail: () -> Unit,
    toEditMe: () -> Unit,
    toRegister: () -> Unit,
    toCollection: () -> Unit,
) {
    MainScreen(
        toLogin = toLogin,
        toMain = toMain,
        toProfile = toProfile,
        toFriend = toFriend,
        meViewModel = meViewModel,
        toMe = toMe,
        toPost = toPost,
        toNotification = toNotification,
        toFollow = toFollow,
        toSearchDetail = toSearchDetail,
        toEditMe = toEditMe,
        toRegister = toRegister,
        toCollection = toCollection,
    )
}
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
@Composable
fun MainScreen(
    toFriend: () -> Unit,
    toProfile: (String) -> Unit,
    toLogin: () -> Unit,
    toMain: () -> Unit,
    toMe: () -> Unit,
    viewModel: HomeViewModel = viewModel(),
    meViewModel: MeViewModel,
    toPost: (String, Int) -> Unit,
    toNotification: () -> Unit,
    toFollow: (String, String) -> Unit,
    toSearchDetail: () -> Unit,
    mainViewModel: MainViewModel = viewModel(),
    toEditMe: () -> Unit,
    toRegister: () -> Unit,
    toCollection: () -> Unit,
) {


    val currentUser = viewModel.currentUserInfo

    // 从ViewModel获取当前destination
    val currentDestination = mainViewModel.currentMainDestination

    // 使用pagerState同步页面状态
    val pageState = rememberPagerState(
        initialPage = TopLevelDestination.entries.indexOfFirst { it.route == currentDestination },
        pageCount = { TopLevelDestination.entries.size }
    )

    // 同步pager状态到ViewModel
    LaunchedEffect(pageState.currentPage) {
        val destination = TopLevelDestination.entries[pageState.currentPage].route
        mainViewModel.updateMainDestination(destination)
    }

    val scope = rememberCoroutineScope()
    val (bottomNavBackgroundColor, setBottomNavBackgroundColor) = remember { mutableStateOf(Color.White) }
    Scaffold(
        bottomBar = {
            MyNavigationBar(
                destinations = TopLevelDestination.entries,
                currentDestination = currentDestination,
                onNavigateToDestination = { index ->
                    scope.launch {
                        pageState.scrollToPage(index)
                    }
                },
                modifier = Modifier
                    .fillMaxWidth()
                    .background(bottomNavBackgroundColor)
                    .navigationBarsPadding(),
                currentUserAvatar = currentUser?.avatar
            )
        }
    ) { padding ->

        Column {
            HorizontalPager(
                state = pageState,
                modifier = Modifier
                    .padding(padding)
                    .fillMaxSize()
                    .weight(1f),
                userScrollEnabled = false
            ) { page ->
                when (page) {
                    0 -> HomeRoute(
                        toFriend = toFriend,
                        toProfile = toProfile,
                        toNotification = toNotification,
                    )
                    1 -> SearchRoute(
                        toSearchDetail = toSearchDetail,
                    )
                    2 -> AddMediaRoute(toMain = toMain)
                    3 -> VideoRoute(
                        toProfile = toProfile
                    )
                    4 -> MeRoute(
                        toProfile = toProfile,
                        toLogin = toLogin,
                        toMain = toMain,
                        toPost = toPost,
                        toFollow = toFollow,
                        toEditMe = toEditMe,
                        toRegister = toRegister,
                        toCollection = toCollection,
                    )
                }
            }

            LaunchedEffect(currentDestination) {
                setBottomNavBackgroundColor(
                    if (currentDestination == VIDEO_ROUTE) Color.Black else Color.White
                )
            }
        }
    }

}

